package cn.maszy.service.impl.investment;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import cn.maszy.common.DateUtils;
import cn.maszy.common.Pager;
import cn.maszy.common.SessionFactory;
import cn.maszy.dao.investment.B_T_Ufp_WorkMapper;
import cn.maszy.model.investment.B_T_Ufp_Work;
import cn.maszy.model.investment.B_T_Ufp_WorkExample;
import cn.maszy.model.investment.B_T_Ufp_WorkExample.Criteria;
import cn.maszy.service.investment.B_T_Ufp_WorkService;


@Service
public class B_T_Ufp_WorkServiceImpl implements B_T_Ufp_WorkService{
	@Autowired
	private B_T_Ufp_WorkMapper b_T_Ufp_WorkMapper;
	

	public B_T_Ufp_Work selectOne(String uuid) {
		B_T_Ufp_Work record=b_T_Ufp_WorkMapper.selectByPrimaryKey(uuid);
		return record;
	}
	public int delete(String checkIds) {
		// TODO Auto-generated method stub
		for (String id : checkIds.split(", ")) {
			b_T_Ufp_WorkMapper.deleteByPrimaryKey(id);
		}
		return 1;
	}

	public int count(B_T_Ufp_WorkExample example,B_T_Ufp_WorkMapper b_T_Ufp_WorkMapper){
		return b_T_Ufp_WorkMapper.countByExample(example);
	}
	public List<B_T_Ufp_Work> statistics(String strattime,String endtime){
		String sql=null;
		if(strattime!=null){
			 sql="SELECT worktime,depName,workname,COUNT(late) AS late,COUNT(leaveearly) AS leaveearly,COUNT(abusinesstravel) AS abusinesstravel FROM b_t_ufp_work"+" "+"where"+" "+"worktime  BETWEEN"+"'"+strattime+"'"+"AND"+"'"+endtime+"'"+"GROUP BY workid,workname";
		}else{
			 sql="SELECT worktime,depName,workname,COUNT(late) AS late,COUNT(leaveearly) AS leaveearly,COUNT(abusinesstravel) AS abusinesstravel FROM b_t_ufp_work"+" "+"where"+" "+"worktime  BETWEEN"+"'"+DateUtils.getMonthFirstDay()+"'"+"AND"+"'"+ DateUtils.getMonthLastDay()+"'"+"GROUP BY workid,workname";
		}
		B_T_Ufp_WorkExample example=new B_T_Ufp_WorkExample();
		example.setOrderByClause(sql);
		List<B_T_Ufp_Work>  list=b_T_Ufp_WorkMapper.selectByExamplestatistics(example);
		return list;
		
		
	}
 
	

	public Pager<B_T_Ufp_Work> selectList(B_T_Ufp_Work b_T_Ufp_Work,Pager<B_T_Ufp_Work> pager,String strattime,String endtime){
		B_T_Ufp_WorkExample example=new B_T_Ufp_WorkExample();
		Criteria criteria = example.createCriteria();
		//搜索条件
		if(strattime!=null && !"".equals(strattime)){
			criteria.andWorktimeGreaterThanOrEqualTo(strattime);
			}
		if(endtime!=null && !"".equals(endtime)){
			criteria.andWorktimeLessThanOrEqualTo(endtime);
			}
		if(b_T_Ufp_Work != null && !"".equals(b_T_Ufp_Work)){
			if(b_T_Ufp_Work.getWorkname()!=null&&!"".equals(b_T_Ufp_Work.getWorkname())){
				criteria.andWorknameLike("%"+b_T_Ufp_Work.getWorkname()+"%");
			}
		}else{
			criteria.andUuidIsNotNull();
		}
		if(strattime == null || "".equals(strattime) || endtime == null || "".equals(endtime)){
			criteria.andWorktimeEqualTo(DateUtils.now());
		}
		int totalCount=new B_T_Ufp_WorkServiceImpl().count(example,b_T_Ufp_WorkMapper);
		pager.setTotalRecords(totalCount);
		example.setLimitStart(pager.getStartRecord());
		example.setLimitEnd(pager.getPageSize());
		example.setOrderByClause(pager.getOrderField()+" "+pager.getOrderDirection());
		// 取得总的记录条数
		List<B_T_Ufp_Work>  list=b_T_Ufp_WorkMapper.selectByExample(example);
		pager.setPageRecords(list);
		return pager;
	}

	public int update(B_T_Ufp_Work b_T_Ufp_Work) {
		B_T_Ufp_Work ac= new B_T_Ufp_Work();
		String ti = DateUtils.time();
		String s2="09:00";
		int t = ti.compareTo(s2);
			 ac.setWorkstime(ti);
		 if(t<=0){
			 ac.setAfk("正常上班");
		 		}
		 if(t>0){
			 ac.setLate("迟到");
		 }
		 ac.setUuid(UUID.randomUUID().toString());
		 ac.setWorkid(SessionFactory.getSessionUserId());
		 ac.setDepname(SessionFactory.getSessionUserDepNam());
		 ac.setWorkname(SessionFactory.getSessionUserRealNam());
		 ac.setWorktime(DateUtils.now());
		 ac.setIsDelete(0);
		 ac.setCreateUser(SessionFactory.getSessionUserRealNam());
		 return	 b_T_Ufp_WorkMapper.insert(ac);
	 }
	public Pager<B_T_Ufp_Work> selectstatisticsList(B_T_Ufp_Work b_T_Ufp_Work,Pager<B_T_Ufp_Work> pager,String strattime,String endtime) {
		pager.setPageRecords(statistics(strattime,endtime));
		return  pager;
	}
	public B_T_Ufp_Work selectOne1(B_T_Ufp_WorkExample example) {
		example.or().andWorktimeEqualTo(DateUtils.now()).andWorkidEqualTo(SessionFactory.getSessionUserId());
		List<B_T_Ufp_Work> record=b_T_Ufp_WorkMapper.selectByExample(example);
		B_T_Ufp_Work b_T_Ufp_Work = new B_T_Ufp_Work();
		b_T_Ufp_Work = record.get(0);
		return b_T_Ufp_Work;
	}
	public int updatenext(B_T_Ufp_Work b_T_Ufp_Work) {
		B_T_Ufp_WorkExample example=new B_T_Ufp_WorkExample();
		b_T_Ufp_Work = selectOne1(example);
		B_T_Ufp_Work ac= new B_T_Ufp_Work();
		String ti = DateUtils.time();
		String s3="17:30";
		int t = ti.compareTo(s3);
		if (t>=0){
			ac.setAbusinesstravel("正常下班");
		}else{
			ac.setLeaveearly("早退");
		}
		ac.setUuid(b_T_Ufp_Work.getUuid());
		ac.setWorketime(DateUtils.time());
		ac.setWorkstime(b_T_Ufp_Work.getWorkstime());
		ac.setWorkid(SessionFactory.getSessionUserId());
		ac.setDepname(SessionFactory.getSessionUserDepNam());
		ac.setWorkname(SessionFactory.getSessionUserRealNam());
		ac.setWorktime(DateUtils.now());
		ac.setLate(b_T_Ufp_Work.getLate());
		return	b_T_Ufp_WorkMapper.updateByPrimaryKey(ac);
		
	}
	public B_T_Ufp_Work selectwork(String uuid) {
		B_T_Ufp_WorkExample example = new B_T_Ufp_WorkExample();
		example.or().andWorktimeEqualTo(DateUtils.now()).andWorkidEqualTo(uuid);
		List<B_T_Ufp_Work> record=b_T_Ufp_WorkMapper.selectByExample(example);
		B_T_Ufp_Work b_T_Ufp_Work = new B_T_Ufp_Work();
		if(record.size()>0){
			b_T_Ufp_Work = record.get(0);
		}else{
		}
		return b_T_Ufp_Work;
	}
	}


	
	

	

